// MFCSocketDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFCSocket_Server.h"
#include "MFCSocketDlg_Server.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCSocketDlg_Server dialog




CMFCSocketDlg_Server::CMFCSocketDlg_Server(CWnd* pParent /*=NULL*/)
	: CDialog(CMFCSocketDlg_Server::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMFCSocketDlg_Server::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMFCSocketDlg_Server, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTN_START, &CMFCSocketDlg_Server::OnBnClickedBtnStart)
	ON_BN_CLICKED(IDC_BTN_STOP, &CMFCSocketDlg_Server::OnBnClickedBtnStop)
END_MESSAGE_MAP()


// CMFCSocketDlg_Server message handlers

BOOL CMFCSocketDlg_Server::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	GetDlgItem(IDC_BTN_START)->EnableWindow(TRUE);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(FALSE);
	m_port = 2000;

	m_clientSocket.pDlg = this;
	m_socketServer.pDlg = this;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMFCSocketDlg_Server::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMFCSocketDlg_Server::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//////////////////////////////////////////////////////////////////////////
void CMFCSocketDlg_Server::SocketStartServer()
{
	OutputDebugString(_T("[Server Dlg]: start server"));
	if( AfxSocketInit() == FALSE)
	{ 
		AfxMessageBox(_T("Failed to Initialize Sockets")); 
		return; 
	}

	/*if(m_Server.Create(1001)==FALSE)*/
	if(m_socketServer.Create(m_port) == FALSE)
	{
		MessageBox(_T("Failed to create"));
		return;
	}
	
	/*if(m_Server.Listen()==FALSE)*/
	if(m_socketServer.Listen() == FALSE)
	{
		MessageBox(_T("Failed"));
		m_socketServer.Close();
		return;
	}

	GetDlgItem(IDC_BTN_START)->EnableWindow(false);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(true);
	//((CMFCSocketDlg_Server*)m_socketServer.m_clientSocket.pDlg) = this;
}

void CMFCSocketDlg_Server::SocketAccept()
{
	OutputDebugString(_T("[Server Dlg]: accept client"));
	/*
	m_socketServer.m_clientSocket.m_hSocket = INVALID_SOCKET;
	m_socketServer.Accept(m_socketServer.m_clientSocket);
	*/
	/*
	m_clientSocket.m_hSocket  =INVALID_SOCKET;
	m_socketServer.Accept(m_clientSocket);
	*/
	//MessageBox(_T("Client Accepted"));
	SetWindowText(_T("Server listening..."));
}

void CMFCSocketDlg_Server::SocketReceiveMessage()
{
	
}

void CMFCSocketDlg_Server::SocketSendMessage()
{

}

void CMFCSocketDlg_Server::SocketStopServer()
{
	/*
#ifdef USE_MULTI_CLIENTS
	if(m_Server.m_clientCurrentID >= 0)
	{
		m_Server.m_aClients[m_Server.m_clientCurrentID].ShutDown(2);
	}
#else
	m_Server.m_Client.ShutDown(2);
#endif
	m_Server.Close();
	*/
	m_socketServer.Close();

	GetDlgItem(IDC_BTN_START)->EnableWindow(true);
	GetDlgItem(IDC_BTN_STOP)->EnableWindow(false);
	SetWindowText(_T("Server stoped"));
}

void CMFCSocketDlg_Server::OnBnClickedBtnStart()
{
	SocketStartServer();
}

void CMFCSocketDlg_Server::OnBnClickedBtnStop()
{
	SocketStopServer();
}
